package cn.swenty.backstage.framework.common.util;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 * 
 * @author 孙洋
 * @since Apr 15, 2009 11:38:16 AM
 */
public class JavaTypeUtil {
	private static Logger log=Logger.getLogger(JavaTypeUtil.class);
	public static Class[] SIMPLE = {void.class, boolean.class,int.class, Integer.class, long.class,
			Long.class, double.class, Double.class, float.class,Float.class, String.class,
			Date.class,Timestamp.class,java.sql.Date.class,java.lang.Boolean.class};
	
	public static Class[] COLLECTION = {List.class,Set.class,ArrayList.class,HashSet.class};

	/**将数据库类型转换为java类型
	 * @param length  当字段是数字时,超过多少位的转为long*/
	public static Class parseDb(String dbType, int length) {
		String type = dbType.toUpperCase();
		if (type.startsWith("VARCHAR")) {
			return String.class;
		}
		if (type.startsWith("DATE")) {
			return Date.class;
		}
		if (type.startsWith("CLOB")) {
			return String.class;
		}
		if (type.startsWith("NUMBER")) {
			if (length >= 10) {
				return Long.class;
			} else {
				return Integer.class;
			}
		}
		throw new RuntimeException("未知的数据库类型"+dbType);
	}

	/** 判断一个对象是否简单类型 */
	public static boolean isSimple(Object o) {
		if (o == null) {
			return true;
		}
		if(o instanceof Date){
			return true;
		}
		return isSimple(o.getClass());
	}

	/**判断你一个类是否基本类型，*/
	public static boolean isSimple(Class clazz) {
		for(Class cls:JavaTypeUtil.SIMPLE){
			if(clazz==cls){
				return true;
			}
		}
		return false;
	}

	/** 判断一个对象是否数组 */
	public static boolean isArray(Object obj) {
		if (obj instanceof int[]) {
			return true;
		}
		if (obj instanceof long[]) {
			return true;
		}
		if (obj instanceof double[]) {
			return true;
		}
		if (obj instanceof float[]) {
			return true;
		}
		if (obj instanceof Object[]) {
			return true;
		}
		return false;
	}

	/** 将一个简单类型数据替换成对象数组 */
	public static Object[] toObjectArray(Object obj) {
		if (obj instanceof int[]) {
			int[] o = (int[]) obj;
			Integer[] is = new Integer[o.length];
			for (int i = 0; i < o.length; i++) {
				is[i] = o[i];
			}
			return is;
		}
		if (obj instanceof long[]) {
			long[] o = (long[]) obj;
			Long[] is = new Long[o.length];
			for (int i = 0; i < o.length; i++) {
				is[i] = o[i];
			}
			return is;
		}
		if (obj instanceof double[]) {
			double[] o = (double[]) obj;
			Double[] is = new Double[o.length];
			for (int i = 0; i < o.length; i++) {
				is[i] = o[i];
			}
			return is;
		}

		if (obj instanceof float[]) {
			float[] o = (float[]) obj;
			Float[] is = new Float[o.length];
			for (int i = 0; i < o.length; i++) {
				is[i] = o[i];
			}
			return is;
		}
		return (Object[]) obj;
	}

	/**
	 * 获得某个字段的声明所对应的泛型
	 * 
	 */
	public static Class getGenericType(Class clazz, String field) {
		String methodName = "get" + StringUtil.firstUp(field);
		try {
			Method method = clazz.getMethod(methodName);
			//通过返回值
			Type returnType = method.getGenericReturnType();
			if (returnType instanceof ParameterizedType) {
				Type[] types = ((ParameterizedType) returnType)
						.getActualTypeArguments();
				for (Type type : types) {
					return (Class) type;
				}
			}
			return null;
			// 通过参数
			// Type[] paramTypeList = method.getGenericParameterTypes();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}
	
	/**将数据库形式的字段名转为java格式的*/
	public static String toJavaName(String dbName){
		StringBuffer sb=new StringBuffer();
		String[] strs=dbName.split("_");
		for(String str:strs){
			sb.append(StringUtil.firstUpOnly(str));
		}
		return StringUtil.firstLower(sb.toString());
	}
	
	/**将数据库形式的字段名转为java格式的*/
	/** 将符合java的命名转为数据库的命名 */
	public static String toDatabaseName(String code) {
		code=StringUtil.firstLower(code);
		return StringUtil.pareseUpCase(code);
	}

	
	/**class a是否继承自class b*/
	public static boolean isInstance(Class a,Class b){
		if(a==b){
			return true;
		}
		
		for(Class inter:a.getInterfaces()){
			if(isInstance(inter,b)){
				return true;
			}				
		}
		
		Class supe=a.getSuperclass();
		while(supe!=null){
			if(supe==b){
				return true;
			}else{
				supe=supe.getSuperclass();
			}
		}
		return false;
	}
	
	
	
	
	
	/**判断一个类是否是抽象抽象的，接口也算*/
	public static boolean isAbstrace(Class clazz){
		return Modifier.isAbstract(clazz.getModifiers());
	}
	
	/**获得某个类所有实现的接口*/
	public static Set<Class> getAllInterface(Class clazz){
		Set set=new HashSet();
		addInterface(set,clazz);	
		return set;
	}

	
	
	private static void addInterface(Set set ,Class clazz){
		if(clazz.isInterface()){
			set.add(clazz);
		}
		Class parent=clazz.getSuperclass();
		if(parent!=null&&parent!=Object.class){
			addInterface(set,clazz);
		}
		for(Class in:clazz.getInterfaces()){
			addInterface(set,in);
		}
	}
}


